Skip to content

bpo-32751: Wait for task cancellation in asyncio.wait_for()#7216

Merged
1st1 merged 3 commits intopython:masterfrom
elprans:bpo-32751
May 29, 2018
Merged

bpo-32751: Wait for task cancellation in asyncio.wait_for()#7216
1st1 merged 3 commits intopython:masterfrom
elprans:bpo-32751

Conversation

@elprans
Copy link
Contributor

@elprans elprans commented May 29, 2018

Currently, asyncio.wait_for(fut), upon reaching the timeout deadline,
cancels the future and returns immediately. This is problematic when
fut is a Task, because it will be left running for an arbitrary
amount of time. This behavior is iself surprising and may lead to
related bugs such as the one described in bpo-33638:

condition = asyncio.Condition()
async with condition:
    await asyncio.wait_for(condition.wait(), timeout=0.5)

Currently, instead of raising a TimeoutError, the above code will fail
with RuntimeError: cannot wait on un-acquired lock, because
__aexit__ is reached before condition.wait() finishes its
cancellation and re-acquires the condition lock.

To resolve this, make wait_for await for the task cancellation.
The tradeoff here is that the timeout promise may be broken if the
task decides to handle its cancellation in a slow way. This represents
a behavior change and should probably not be back-patched to 3.6 and
earlier.

https://bugs.python.org/issue32751

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants